﻿
function JavaScriptCodeBehind() {};

JavaScriptCodeBehind.FindControl = function(serverID, recursive) {
    for (var i = 0; i < this.Controls.length; i++) {
        if (this.Controls[i].ServerID == serverID) return this.Controls[i];
    }
    
    if(typeof(recursive) === 'undefined' || recursive) {        
        var control = null;
        for (var i = 0; i < this.Controls.length; i++) {
            control = this.Controls[i].FindControl(serverID, true);
            if(control != null)
                return control;
        }
        
    }
    
    return null;
};

JavaScriptCodeBehind.CallHandler = function(handler, sender, p1, p2, p3, p4, p5) {
    this._handler = handler;
    return this._handler(sender, p1, p2, p3, p4, p5);
};

JavaScriptCodeBehind.CreateHandler = function(handler) {
    var b = function(p1, p2, p3, p4, p5) {
        if (typeof (handler) !== 'undefined') {
            b._this._handler = handler;
            return b._this._handler(this, p1, p2, p3, p4, p5);
        }
    };
    b._this = this;    
    return b;
};

JavaScriptCodeBehind.AddHandler = function(element, eventName, handler) {
    var b = function(p1, p2, p3, p4, p5) {
        if (typeof (handler) !== 'undefined') {
            b._this._handler = handler;
            return b._this._handler(b._sender, p1, p2, p3, p4, p5);
        }
    };
    b._this = this;
    b._sender = element;
    $addHandler(element, eventName, b);
};

JavaScriptCodeBehind.PerformMetaProgrammingArray = function(a, b, cID, root) {
    var c;

    var d = b != null ? b.FindControl(a[0], false) : null;

    if ((a[1] != null) && (a[5] === 'undefined'))
        throw new Error('The prototype of control is not declared in the JavaScriptCodeBehind file');

    if (a[1] != null) {
        if (a[0] == 'Page' && typeof (Page) !== 'undefined') {
            c = Page;
        }
        else if ((b == null) || (d == null)) {
            c = a[1]();
            c.FindControl = JavaScriptCodeBehind.FindControl;
            c.addHandler = JavaScriptCodeBehind.AddHandler; // Obsoleto...
            c.AddHandler = JavaScriptCodeBehind.AddHandler;
            c.Controls = new Array();
        }
        else {
            c = d;
        }
        cID = cID == '' ? c.ServerID == 'Page' ? '' : c.ServerID : cID + '_' + c.ServerID;
        c.CreateHandler = JavaScriptCodeBehind.CreateHandler;
        c.CallHandler = JavaScriptCodeBehind.CallHandler;
        c.ClientID = cID;
    }
    else {
        cID = cID == '' ? a[0] == 'Page' ? '' : a[0] : cID + '_' + a[0];
        if (a[0] == 'Page' && typeof (Page) !== 'undefined') {
            c = Page;
        }
        else if ((b == null) || (d == null)) {
            c = $get(cID);
            c = c == null ? new Object() : c;
            c.FindControl = JavaScriptCodeBehind.FindControl;
            c.ServerID = a[0];
            c.Controls = new Array();

            if (a[0] == 'Page')
                Page = c;

        }
        else {
            c = d;
        }
        c.CreateHandler = JavaScriptCodeBehind.CreateHandler;
        c.CallHandler = JavaScriptCodeBehind.CallHandler;
        c.ClientID = cID;
    }

    // Se o novo controle é uma nova raiz de controles
    if (a[4]) {
        var _root = root;
        root = c;
    }

    // Se o controle raiz é o controle pai do novo controle
    if (root == b) {
        if (typeof (b[c.ServerID]) === 'undefined') {
            b.Controls[b.Controls.length] = c;
            b[c.ServerID] = c;
        }
    }
    else {

        // Se o novo controle não é a nova raiz da árvore de controles
        if (root == c) {
            // Se o novo controle deve ser referenciado pelo controle raiz
            if (a[3] && _root != null) {
                _root[c.ServerID] = c;
            }
        }
        else {
            // Se o novo controle deve ser referenciado pelo controle raiz
            if (a[3]) {
                root[c.ServerID] = c;
            }
        }

        // Se existe um controle pai e o controle pai ainda não possui um atributo referenciando o novo controle
        if (b != null && typeof (b[c.ServerID]) === 'undefined') {
            b.Controls[b.Controls.length] = c;
            b[c.ServerID] = c;
        }

    }

    var i;
    if (a[2] != null) {
        for (i = 0; i < a[2].length; i++) {
            JavaScriptCodeBehind.PerformMetaProgrammingArray(a[2][i], c, cID, root);
        }
    }

    // A raiz dos controles é novamente a raiz de controle do controle pai
    root = _root;

    if (a[1] != null && typeof (JavaScriptCodeBehind.ToRunIndex[cID]) === 'undefined') {
        JavaScriptCodeBehind.ToRunIndex[cID] = c;
        JavaScriptCodeBehind.ToRun[JavaScriptCodeBehind.ToRun.length] = c;
    }
}

JavaScriptCodeBehind.ToRunIndex=null;
JavaScriptCodeBehind.ToRun=null;

JavaScriptCodeBehind.Initialize = function() {

    JavaScriptCodeBehind.ToRunIndex = new Object();
    JavaScriptCodeBehind.ToRun = new Array();
    Page = undefined;

    for (var i = 0; i < JSCB$MetaProgrammingDataArray.length; i++) {
        JavaScriptCodeBehind.PerformMetaProgrammingArray(JSCB$MetaProgrammingDataArray[i], null, '', null);
    }

    for (var i = 0; i < JavaScriptCodeBehind.ToRun.length; i++) {
        JavaScriptCodeBehind.ToRun[i].main();
    }
}

JavaScriptCodeBehind.Finalize = function() {
    JSCB$MetaProgrammingDataArray = [];
}